rm(list = ls())

# Load Packages
library(tidyverse) # 1.3.1
library(stargazer) # 5.2.2
library(scales) # 1.1.1
library(modelsummary) # 0.7.0
library(vtable) # 1.3.1
library(gtsummary) # 1.4.1
library(gridExtra) # 2.3
library(jtools) # 2.1.3
library(sandwich) # 3.0-1


# Load study 1
study1 <- read.csv('perspective_taking_study1.csv')

# Add indicator to study 1
study1$study <- rep(1, length = nrow(study1))

# Load study 2
study2 <- read.csv('perspective_taking_study2.csv')

# Add indicator to study 2
study2$study <- rep(2, length = nrow(study2))

# Change variables to match
study2$s1_control_undoc = study2$s1_control_covid

# Merge them
merge(study1, study2, all = T) -> study_merge


################################################
########### Deservingness ######################
################################################

study_merge <- study_merge %>%
  rename(exp1_control_lowinc = s1_control_lowinc,
         exp1_lowinc = s1_lowinc,
         exp1_control_undoc = s1_control_undoc,
         exp1_undoc = s1_undoc,
         exp1_cancer = s1_cancer,
         exp1_immigrant = s1_immigrant)

## Add treatment indicator
study_merge$treatment <- NA
study_merge$treatment[study_merge$exp1_control_lowinc >= 0 & study_merge$exp1_control_lowinc <= 1] <- "Control Low Inc"
study_merge$treatment[study_merge$exp1_lowinc >= 0 & study_merge$exp1_lowinc <= 1] <- "Treat Low Inc"
study_merge$treatment[study_merge$exp1_control_undoc >= 0 & study_merge$exp1_control_undoc <= 1] <- "Control Undoc"
study_merge$treatment[study_merge$exp1_undoc >= 0 & study_merge$exp1_undoc <= 1] <- "Treat Undoc"
study_merge$treatment[study_merge$exp1_cancer >= 0 & study_merge$exp1_cancer <= 1] <- "Cancer"
study_merge$treatment[study_merge$exp1_immigrant >= 0 & study_merge$exp1_immigrant <= 1] <- "Immigrant"


# Identify columns
# Control Low Income
which( colnames(study_merge)=="exp1_control_lowinc")

# Treat Low Income
which( colnames(study_merge)=="exp1_lowinc")

# Control Undoc
which( colnames(study_merge)=="exp1_control_undoc")

# Treat Undoc
which( colnames(study_merge)=="exp1_undoc")

# Treat Undoc + Cancer
which( colnames(study_merge)=="exp1_cancer")

# Treat Immigrant
which( colnames(study_merge)=="exp1_immigrant")


# Merge columns into one
study_merge <- study_merge %>%
  unite("response_deserving", c(54, 56, 30, 32, 90, 92) , remove = FALSE, sep = "", na.rm=T) 


# Fill NAs
study_merge[study_merge == ""] <- NA

# Keep as NAs
study_merge %>% 
  mutate_all(na_if,"") -> study_merge

## Order treatments
study_merge %>%
  mutate(treatment = factor(treatment,
                            levels = c("Control Undoc",
                                       "Treat Undoc",
                                       "Cancer",
                                       "Immigrant",
                                       "Control Low Inc",
                                       "Treat Low Inc"))) %>%
  mutate(response_deserving = as.numeric(response_deserving),
         pid_3level = recode(pid_7level,
                             `1` = 1, # Solid Democrat
                             `2` = 1, # Democrat
                             `3` = 1, # Lean Democrat
                             `4` = 2, # Independent 
                             `5` = 3, # Lean Republican
                             `6` = 3, # Republican
                             `7` = 3),# Solid Republican
         polid_3level = recode(pol_ideology,
                               `1` = 1, # Extremely Liberal
                               `2` = 1, # Liberal
                               `3` = 1, # Lean Liberal
                               `4` = 2, # Moderate
                               `5` = 3, # Lean Conservative
                               `6` = 3, # Conservative
                               `7` = 3)) -> study_merge

# Create indicator for treatment
study_merge %>%
  mutate(treated = ifelse(treatment == "Control Undoc" |
                            treatment == "Control Low Inc", "Control", "Treated")) -> study_merge


#####################################   
#####################################   
########### Policy Support ########## 
#####################################   
#####################################

# recode
study_merge <- study_merge %>%
  rename(exp2_control_lowinc = s2_control_lowinc,
         exp2_lowinc = s2_lowinc,
         exp2_control_undoc = s2_control_undoc,
         exp2_undoc = s2_undoc,
         exp2_welfare = s2_policy_welf,
         exp2_health = s2_policy_health,
         exp2_health_welfare = s2_policy_hlth_Welf)


## Add treatment indicator
study_merge$treatment_s2 <- NA
study_merge$treatment_s2[study_merge$exp2_control_lowinc >= 0 & study_merge$exp2_control_lowinc <= 1] <- "Control Low Inc"
study_merge$treatment_s2[study_merge$exp2_lowinc >= 0 & study_merge$exp2_lowinc <= 1] <- "Treat Low Inc"
study_merge$treatment_s2[study_merge$exp2_control_undoc >= 0 & study_merge$exp2_control_undoc <= 1] <- "Control Undoc"
study_merge$treatment_s2[study_merge$exp2_undoc >= 0 & study_merge$exp2_undoc <= 1] <- "Treat Undoc"
study_merge$treatment_s2[study_merge$exp2_welfare >= 0 & study_merge$exp2_welfare <= 1] <- "Welfare"
study_merge$treatment_s2[study_merge$exp2_health >= 0 & study_merge$exp2_health <= 1] <- "Health"
study_merge$treatment_s2[study_merge$exp2_health_welfare >= 0 & study_merge$exp2_health_welfare <= 1] <- "Health & Welfare"

##### Add response indicator

### Get the column of each undoc group
# Control Undoc Pooled
which( colnames(study_merge)=="exp2_control_undoc" )
# Study 1 Control Low Inc
which( colnames(study_merge)=="exp2_control_lowinc" )
# Treatment Undoc Pooled
which( colnames(study_merge)=="exp2_undoc" ) 
# Treatment Low Inc
which( colnames(study_merge)=="exp2_lowinc" ) 

# Study 2 Health
which( colnames(study_merge)=="exp2_health")
# Study 2 Welfare
which( colnames(study_merge)=="exp2_welfare" )
# Study 2 Health and Welfare
which( colnames(study_merge)=="exp2_health_welfare" )

# Merge into one column
study_merge <- study_merge %>%
  unite("response_policy", c(58, 59, 61, 60, 94, 97, 100) , remove = FALSE, sep = "", na.rm=T) 

# Fill NAs
study_merge[study_merge == ""] <- NA

#
study_merge %>%
  mutate(response_policy = as.numeric(response_policy)) -> study_merge

## save dataset to use in analyses

write.csv(study_merge, "perspective_taking_merged.csv",
          row.names = FALSE)

